﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SystemCenterSoa.BaseLibrary.Tools.TypeTools;
using SystemCenterSoa.DtoModel.Common;
using SystemCenterSoa.PoModel.SystemManager;
using SystemCenterSoa.Service.SystemManager;

namespace SystemCenterSoa.ApiProject.Area.SystemManager
{
    public class DictionariesController : SystemManagerBaseController
    {
        private DictionariesService _dictionariesService;
        public DictionariesController(DictionariesService dictionariesService)
        {
            _dictionariesService = dictionariesService;
        }
        /// <summary>
        /// 根据字典类别key获取下面的字典选项
        /// </summary>
        /// <param name="typeKey">字典类别key</param>
        /// <returns></returns>
        [HttpGet]
        public ResultModel<List<DictionariesPo>> GetDictionariesKey(string typeKey)
        {
            try
            {
                var data = _dictionariesService.GetDictionariesKey(typeKey);
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<List<DictionariesPo>>(ex);
            }
        }
        /// <summary>
        /// 分页条件查询
        /// </summary>
        /// <param name="dicName"></param>
        /// <param name="dicType"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetDictionariesList(string dicName, string dicType, int pageIndex, int pageSize)
        {
            try
            {
                var query = _dictionariesService.GetDictionariesList(dicName, dicType, pageIndex, pageSize);
                var total = query.Count();
                var list = query.OrderBy(t => t.dic_id).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                return Ok(new { list, total });
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="dictionaries"></param>
        /// <returns></returns>
        [HttpPost]
        public bool GetDictionariesAdd(DictionariesPo dictionaries)
        {
            return _dictionariesService.GetDictionariesAdd(dictionaries);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public bool GetDictionariesDel(string ids)
        {
            return _dictionariesService.GetDictionariesDel(ids);
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public DictionariesPo GetDictionariesFan(int id)
        {
            return _dictionariesService.GetDictionariesFan(id);
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="dictionaries"></param>
        /// <returns></returns>
        [HttpPost]
        public bool GetDictionariesUpdate(DictionariesPo dictionaries)
        {
            return _dictionariesService.GetDictionariesUpdate(dictionaries);
        }
    }
}
